<?php

namespace app\controllers;

use app\models\Area;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\Loan;

class ChedaiController extends Controller
{

    public function beforeAction($action)
    {
        $this->enableCsrfValidation = false;
        return parent::beforeAction($action);
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }



    /**
     * @return string
     */
    public function actionGetProvinceList()
    {
        $province = Area::find()
            ->select("id, name, level")
            ->where(['pid' => 0, 'level' => 1])
            ->orderBy('id')
            ->asArray()
            ->all();
        return json_encode($province, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @return string
     */
    public function actionGetCityList()
    {
        $pid = Yii::$app->request->get('pid', 0);
        if ($pid == 0) {
            return json_encode(['errno' => -1]);
        }

        $city = Area::find()
            ->select("id, name, level")
            ->where(['pid' => $pid, 'level' => 2])
            ->orderBy('id')
            ->asArray()
            ->all();
        return json_encode(['errno' => 0, 'data' => $city], JSON_UNESCAPED_UNICODE);
    }

    public function actionDoLoan()
    {
        if (Yii::$app->request->post()) {
            $money = Yii::$app->request->post('money', 0);
            $pur = Yii::$app->request->post('pur', 0);
            $type = Yii::$app->request->post('type', 0);
            $name = Yii::$app->request->post('name', 0);
            $province = Yii::$app->request->post('province', 0);
            $city = Yii::$app->request->post('city', 0);
            $mobile = Yii::$app->request->post('mobile', 0);

            if (empty($mobile)) {
                return json_encode(['errno' => -1, '手机号码不能为空']);
            }

            if (empty($name)) {
                return json_encode(['errno' => -1, '联系姓名不能为空']);
            }

            if (empty($money)) {
                return json_encode(['errno' => -1, '贷款金额不能为空']);
            }

            $remark = '';
            if (!empty($type)) {
                $remark .= '车辆购买方式：' . $type . '；';
            }

            if (!empty($province)) {
                $remark .= '所在省份：' . $province . '；';
            }

            if (!empty($city)) {
                $remark .= '所在城市：' . $city . '；';
            }

            $model = new Loan();
            $model->name = $name;
            $model->mobile = $mobile;
            $model->loan_purpose = $pur;
            $model->loan_amount = $money;

            if (!empty($remark)) {
                $model->remark = $remark;
            }

            $model->create_time = time();
            $model->update_time = time();
            $model->ip = Yii::$app->tool->getIp();
            if (!$model->save()) {
                return json_encode(['errno' => -1, '保存失败']);
            }
            return json_encode(['errno' => 0, 'success']);
        }

        return json_encode(['errno' => -1, '请求异常']);
    }
}
